package com.wx.algorithm.linked.node;

/**
 * @desc:
 * @author: wx
 * @time: 2019/12/7 10:48 AM
 */
public class OneNodeList<E> {


    private OneNode<E> head;


    private OneNode<E> tail;

    private OneNode<E> node;

    private int size = 0;

    public void add(E e) {

        if (head == null) {
            head = new OneNode<>(e);
            tail = head;
        } else {
            OneNode<E> oneNode = new OneNode<>(e);
            tail.setNext(oneNode);
            tail = oneNode;
        }
        size++;
    }

    public OneNode<E> getHead(){
        return head;
    }

    public OneNode<E> getTail(){
        return tail;
    }

    public OneNode<E> getNext(){
        if (node == null) {
            node = head;
        }
        OneNode<E> oneNode =  node;
        OneNode<E> next = node.getNext();
        if (next == null) {
            return  null;
        }
        node = next;
        return oneNode;
    }

    public void traverse(){
        OneNode<E> next = getNext();
        while (next != null){
            System.out.println(next.getE());
            next = next.getNext();
        }
    }

    public int getSize() {
        return size;
    }


    public E  get(int index){
        if (index == 0){
            return head.getE();
        }

        int i = 0;
        node = null;
        OneNode<E> next = getNext();
        while (next != null){
            if (index == i){
                return next.getE();
            }
            next = next.getNext();
            i++;
        }
        return null;
    }


    public OneNode<E>  getNode(int index){
        if (index == 0){
            return head;
        }

        int i = 0;
        node = null;
        OneNode<E> next = getNext();
        while (next != null){
            if (index == i){
                return next;
            }
            next = next.getNext();
            i++;
        }
        return null;
    }
}
